package org.ithang.application.bcRecord;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.ithang.application.bcAddr.bean.BcAddr;
import org.ithang.application.bcAddr.service.BcAddrService;
import org.ithang.application.bcRecord.bean.BcRecord;
import org.ithang.application.bcRecord.service.BcRecordService;
import org.ithang.system.keyvalue.bean.Keyvalue;
import org.ithang.system.keyvalue.service.KeyvalueService;
import org.ithang.tools.eth.ETHInfo;
import org.ithang.tools.eth.ETHUtil;
import org.ithang.tools.lang.JsonUtils;
import org.ithang.tools.lang.StrUtils;
import org.ithang.tools.model.Action;
import org.ithang.tools.model.ActionResult;
import org.ithang.tools.model.ActionValues;
import org.ithang.tools.model.ErrorInfo;
import org.ithang.tools.model.Pager;
import org.ithang.tools.redis.Keys;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 充币记录
 * @author zyt 控制器
 *
 */
@Controller
@RequestMapping("/app/bcRecord")
public class BcRecordAction extends Action<BcRecord>{

    @Autowired
    private BcRecordService bcRecordService; 

    @Autowired
    private BcAddrService bcAddrService;
    
    @Autowired
    private KeyvalueService keyvalueService;

    @ResponseBody
	@RequestMapping(value="add",method=RequestMethod.POST)
	public ActionResult add(BcRecord bcRecord){
		bcRecordService.add(bcRecord);
		return success();
	}
	
    @ResponseBody
	@RequestMapping(value="fresh",method=RequestMethod.POST)
	public ActionResult fresh(String txhash){
    	
    	if(!StrUtils.isNotBlank(txhash)){
    		return error(ErrorInfo.InvalidDataError);
    	}
    	
		ETHInfo ei=ETHUtil.detail(txhash.trim());
		String to=ei.getData().get("to");//接收地址
		String value=ei.getData().get("value");//转账金额
		String status=ei.getData().get("status");
		
		BcAddr bcaddr=bcAddrService.getByAddr(to);
		
		if(200!=ei.getStatus()){
			return error(ErrorInfo.QueryTradFailError);
		}
		
		if(null==bcaddr){
			return error(ErrorInfo.RechargeError);
		}
		
		Keyvalue kv=keyvalueService.get(Keys.MinRecharge);
		if(Double.parseDouble(value)<kv.doubleValue()){
			return error(ErrorInfo.MinRechargeError);
		}
		
		
		BcRecord bc=bcRecordService.getTXHash(txhash);
		if(null!=bc){
			return error(ErrorInfo.HasRechargeError);
		}
		
		if("0".equals(status)){//失败
			//充值失败处理
			bcRecordService.rechargeFail(JsonUtils.toJsonStr(ei), value,bcaddr.getCustomer_id(),txhash);
		}
		
		if("1".equals(status)){//成功
			//充值成功处理
			bcRecordService.rechargeSuccess(JsonUtils.toJsonStr(ei), value,bcaddr.getCustomer_id(),txhash);
		}
		
		return success();
	}
    
    @RequestMapping("form")
    public String form(@RequestParam(value="id",required=false)Long id,Model m){
    	if(null!=id){
    		BcRecord bean=bcRecordService.get(id);
    		m.addAttribute(bean);
    	}else{
    		m.addAttribute(new BcRecord());
    	}
    	return "app/bcRecord/form";
    }
	
	
	@ResponseBody
	@RequestMapping(value="update",method=RequestMethod.POST)
	public ActionResult update(BcRecord bcRecord){
		bcRecordService.update(bcRecord);
		return success();
	}


    @ResponseBody
	@RequestMapping(value="get",method=RequestMethod.GET)
	public ActionResult get(Long id){
		BcRecord r=bcRecordService.get(id);
		return success(r);
	}
	
	@ResponseBody
	@RequestMapping(value="delete",method=RequestMethod.POST)
	public ActionResult delete(@RequestParam(value="id",required=false)Long id,@RequestParam(value="ids",required=false)String ids){
		
		if(null!=id&&id>0){
			bcRecordService.delete(id);	
		}
		
		if(null!=ids&&ids.length()>0){
			bcRecordService.batchDelete(ids.split(","));
		}
		
		return success();
	}
	
	@ResponseBody
	@RequestMapping(value="list",method={RequestMethod.GET,RequestMethod.POST})
	public ActionResult list(Long... ids){
		return success(bcRecordService.list(ids));
	}
	
	@ResponseBody
	@RequestMapping(value="page",method=RequestMethod.POST)
	public ActionResult page(BcRecord bcRecord,Pager<BcRecord> page){
	    page.setBean(bcRecord);
	    List<BcRecord> data=bcRecordService.page(page);
	    page.setData(data);
		return success(page);
	}
	
	@ResponseBody
	@RequestMapping(value="query",method=RequestMethod.POST)
	public ActionResult query(HttpServletRequest request){
		ActionValues values=new ActionValues(request);
		return success(bcRecordService.query(values));
	}
     

}